package com.zlx.mall.basic.mask;

import cn.hutool.core.util.DesensitizedUtil;
import com.zlx.mall.basic.mask.model.MaskData;
import com.zlx.mall.basic.util.crypto.AESUtil;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import java.nio.charset.StandardCharsets;

/**
 * taotao
 *
 * @description 数据脱敏加密服务
 */
@Component
public class DataMaskHandler {

    private static final String AES_KEY = "Z@l#X2024_DataAes";

    /**
     * 手机号脱敏
     *
     * @param mobile 手机号
     * @return 脱敏后的手机号 135****5555
     */
    public String maskMobile(String mobile) {
        if (mobile == null || mobile.length() != 11) {
            return mobile;
        }
        return mobile.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2");
    }

    /**
     *  手机号处理
     * @param mobile
     * @return
     */
    public MaskData handleMobile(String mobile) {
        if (StringUtils.isBlank(mobile)) {
            return new MaskData(mobile, null, null, null);
        }
        return new MaskData(mobile, maskMobile(mobile), md5(mobile), encrypt(mobile));
    }

    /**
     * 身份证号脱敏
     *
     * @param idCard
     * @return **************1234
     */
    public String maskIdCard(String idCard) {
        if (idCard == null || idCard.length() != 18) {
            return idCard;
        }
        return idCard.replaceAll("\\d{4}\\d{10}(\\d{4})", "**************$1");
    }

    /**
     * 用户名称脱敏
     *
     * @param userName User Name
     * @return Mask User Name
     */
    public String maskUserName(String userName) {
        Assert.hasText(userName, "User Name can not be empty");
        return DesensitizedUtil.chineseName(userName);
    }

    /**
     *
     * @param idCard
     * @return
     */
    public MaskData handleIdCard(String idCard) {
        if (StringUtils.isBlank(idCard)) {
            return new MaskData(idCard, null, null, null);
        }
        return new MaskData(idCard, maskIdCard(idCard), md5(idCard), encrypt(idCard));
    }

    /**
     * 银行卡脱敏
     * @param bankCard
     * @return 622202*********0000
     */
    public String maskBankCard(String bankCard) {
        return StringUtils.isBlank(bankCard) ? "" : StringUtils.left(bankCard, 6).concat(StringUtils.removeStart(StringUtils.leftPad(StringUtils.right(bankCard, 4), StringUtils.length(bankCard), "*"), "******"));
    }

    /**
     * 银行卡处理
     * @param bankCard
     * @return
     */
    public MaskData handleBankCard(String bankCard) {
        if (StringUtils.isBlank(bankCard)) {
            return new MaskData(bankCard, null, null, null);
        }
        return new MaskData(bankCard, maskBankCard(bankCard), md5(bankCard), encrypt(bankCard));
    }

    /**
     * 用户名称处理
     *
     * @param userName User Name
     * @return Mask Data
     */
    public MaskData handleUserName(String userName) {
        if (StringUtils.isBlank(userName)) {
            return new MaskData(userName, null, null, null);
        }
        return new MaskData(userName, maskUserName(userName), md5(userName), encrypt(userName));
    }

    public String md5(String data) {
        return DigestUtils.md5Hex(data);
    }

    public String encrypt(String data) {
        return AESUtil.encryptToBase64(AESUtil.createKey(AES_KEY), data.getBytes(StandardCharsets.UTF_8));
    }

    public String decrypt(String data) {
        if (StringUtils.isBlank(data)) {
            return data;
        }
        return new String(AESUtil.decryptFromBase64(AESUtil.createKey(AES_KEY), data), StandardCharsets.UTF_8);
    }


}
